using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Helpers.Pagination;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Areas.Admin.ViewModels.Languages;
using System.Data.Common;
using System.Transactions;
using FiltronASP.Helpers.ActionFilters;
using FiltronASP.Logger;
using FiltronASP.Areas.Admin.Helpers.Permissions;
using FiltronASP.Areas.Admin.Consts;

namespace FiltronASP.Areas.Admin.Controllers
{
    public class LanguagesController : BaseController
    {
        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela szablonow akceptowanych przez typy stron
        /// </summary>
        protected IPageTypeAcceptableTemplateDAO _pageTypeAcceptableTemplateDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela szablonow
        /// </summary>
        protected ITemplateDAO _templateDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela zmiennych
        /// </summary>
        protected IVariableDAO _variableDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela modulow
        /// </summary>
        protected IModuleDAO _moduleDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela modulow szablonow
        /// </summary>
        protected IModuleTemplateDAO _moduleTemplateDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela gallerii
        /// </summary>
        protected IGalleryDAO _galleryDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela stron
        /// </summary>
        protected IPageDAO _pageDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela gallerii obrazkow
        /// </summary>
        protected IGalleryImageDAO _galleryImageDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela pozycji menu
        /// </summary>
        protected IMenuItemDAO _menuItemDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela pozycji menu
        /// </summary>
        protected IVariableValueDAO _variableValueDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela uprawnien do wersji jezykowych
        /// </summary>
        protected IUserLanguageDAO _userLanguageDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela uprawnien do grup stron
        /// </summary>
        protected IUserPageTypeDAO _userPageTypeDAO;

         /// <summary>
        /// Konstruktor.
        /// </summary>
        /// <param name="templateDAO">klasa odpowiedzialna za operacja na bazie danych, tabela szablonow</param>
        /// <param name="pageTypeAcceptableTemplateDAO">klasa odpowiedzialna za operacja na bazie danych, tabela szablonow akceptowanych przez typy stron</param>
        public LanguagesController(ITemplateDAO templateDAO, IPageTypeAcceptableTemplateDAO pageTypeAcceptableTemplateDAO,
            IVariableDAO variableDAO, IModuleDAO moduleDAO, IModuleTemplateDAO moduleTemplateDAO, IGalleryDAO galleryDAO,
            IPageDAO pageDAO, IGalleryImageDAO galleryImageDAO, IMenuItemDAO menuItemDAO,
            IVariableValueDAO variableValueDAO, IUserLanguageDAO userLanguageDAO, IUserPageTypeDAO userPageTypeDAO)
        {
            this._templateDAO = templateDAO;
            this._pageTypeAcceptableTemplateDAO = pageTypeAcceptableTemplateDAO;
            this._variableDAO = variableDAO;
            this._moduleDAO = moduleDAO;
            this._moduleTemplateDAO = moduleTemplateDAO;
            this._galleryDAO = galleryDAO;
            this._pageDAO = pageDAO;
            this._galleryImageDAO = galleryImageDAO;
            this._menuItemDAO = menuItemDAO;
            this._variableValueDAO = variableValueDAO;
            this._userLanguageDAO = userLanguageDAO;
            this._userPageTypeDAO = userPageTypeDAO;
        }

        /// <summary>
        /// Konstruktor.
        /// </summary>
        public LanguagesController()
            : this(new TemplateDAO(), new PageTypeAcceptableTemplateDAO(), new VariableDAO(),
            new ModuleDAO(), new ModuleTemplateDAO(), new GalleryDAO(), new PageDAO(), new GalleryImageDAO(), 
            new MenuItemDAO(), new VariableValueDAO(), new UserLanguageDAO(), new UserPageTypeDAO())
        {
        }

        /// <summary>
        /// Akcja dla wyswietlania listy wersji jezykowych.
        /// </summary>
        /// <param name="currentPage">biezaca strona paginacji</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult Index(int? page, int? currentLanguageId)
        {
            if (!PermissionHelper.HasUserGlobalPermissionAccess(this.GetAuthenticatedUserId()))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (page <= 0) {
                page = 1;
            }   //if
            Language currentLanguage = null;
            if (!currentLanguageId.HasValue)
            {
                currentLanguage = this._languageDAO.GetDefaultLanguage(this.GetAuthenticatedUserId());
                if (currentLanguage != null)
                {
                    currentLanguageId = currentLanguage.language_id;
                }   //if
            }
            else
            {   //if lang id null
                currentLanguage = this._languageDAO.FindByPK(currentLanguageId);
            }   //else lang id null
            return View(this.GetLanguagesIndexViewModel(page, currentLanguageId));
        }

        /// <summary>
        /// Akcja dla wyswietlania ekranu edycji wersji jezykowej.
        /// </summary>
        /// <param name="languageId">klucz glowny tabeli</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult Edit(int languageId, int? currentLanguageId)
        {
            if (!PermissionHelper.HasUserGlobalPermissionAccess(this.GetAuthenticatedUserId()))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (languageId <= 0)
            {
                return View("NoLanguageFound", this.GetBaseViewModel(currentLanguageId));
            }   //if
            Language language = this._languageDAO.FindByPK(languageId);
            if (language == null)
            {
                return View("NoLanguageFound", this.GetBaseViewModel(currentLanguageId));
            }   //if
            return View(this.GetLanguagesCreateEditDeleteViewModel(currentLanguageId, language));
        }

        /// <summary>
        /// Akcja do zapisu zmian w wersji jezykowej.
        /// </summary>
        /// <param name="language_id">klucz glowny tabeli</param>
        /// <param name="collection">parametry z forma</param>
        /// <returns>widok</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateOnlyIncomingValues]
        [Authorize]
        public ActionResult Edit(LanguagesCreateEditDeleteViewModel viewModel)
        {
            if (!PermissionHelper.HasUserGlobalPermissionAccess(this.GetAuthenticatedUserId()))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (viewModel == null)
            {
                return View("NoLanguageFound", this.GetBaseViewModel(null));
            }   //if null
            if (viewModel.LanguageToOperateWith.language_id <= 0)
            {
                if (viewModel.CurrentLanguage != null)
                {
                    return View("NoLanguageFound", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("NoLanguageFound", this.GetBaseViewModel(null));
            }   //if
            Language language = this._languageDAO.FindByPK(viewModel.LanguageToOperateWith.language_id);
            if (language == null)
            {
                if (viewModel.CurrentLanguage != null)
                {
                    return View("NoLanguageFound", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("NoLanguageFound", this.GetBaseViewModel(null));
            }   //if
            try
            {
                if (ModelState.IsValid)
                {
                    UpdateModel<Language>(language, "LanguageToOperateWith");
                    if (this._languageDAO.SubmitChanges())
                    {
                        if (viewModel.CurrentLanguage != null)
                        {
                            return View("Index", this.GetLanguagesIndexViewModel(viewModel.CurrentLanguage.language_id));
                        }   //
                        Language currentLanguage = this._languageDAO.GetDefaultLanguage(this.GetAuthenticatedUserId());
                        if (currentLanguage != null)
                        {
                            return View("Index", this.GetLanguagesIndexViewModel(currentLanguage.language_id));
                        }   //if
                        return View("Index", this.GetLanguagesIndexViewModel(null));
                    }   //if
                }   //if valid
                if (viewModel.CurrentLanguage != null)
                {
                    return View(this.GetLanguagesCreateEditDeleteViewModel(viewModel.CurrentLanguage.language_id, language));
                }   //if
                return View(this.GetLanguagesCreateEditDeleteViewModel(null, language));
            }   //try
            catch
            {
                if (viewModel.CurrentLanguage != null)
                {
                    return View(this.GetLanguagesCreateEditDeleteViewModel(viewModel.CurrentLanguage.language_id, language));
                }   //if
                return View(this.GetLanguagesCreateEditDeleteViewModel(null, language));
            }   //catch
        }

        /// <summary>
        /// Tworzenie nowej wersji jezykowej.
        /// </summary>
        /// <returns>widok</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult Create(LanguagesIndexViewModel viewModel)
        {
            if (!PermissionHelper.HasUserGlobalPermissionAccess(this.GetAuthenticatedUserId()))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (viewModel == null)
            {
                Logger.MyLogger.Log("LanguagesController exception: Create method, viewModel object null...");
                return View("CannotCreateLanguage", this.GetBaseViewModel(null));
            }   //if null
            Language language = new Language();
            language.language_name = "Nowa wersja językowa";
            language.language_abbr = "nwj";
            language.language_active = false;
            if (viewModel.CurrentLanguage != null)
            {
                return View(this.GetLanguagesCreateEditDeleteViewModel(viewModel.CurrentLanguage.language_id, language));
            }   //if
            return View(this.GetLanguagesCreateEditDeleteViewModel(null, language));
        }

        /// <summary>
        /// Akcja do zapisu nowej wersji jezykowej.
        /// </summary>
        /// <param name="language_id">klucz glowny tabeli</param>
        /// <param name="collection">parametry z forma</param>
        /// <returns>widok</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateOnlyIncomingValues]
        [Authorize]
        public ActionResult CreateNewLanguage(LanguagesCreateEditDeleteViewModel viewModel)
        {
            if (!PermissionHelper.HasUserGlobalPermissionAccess(this.GetAuthenticatedUserId()))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (viewModel == null)
            {
                Logger.MyLogger.Log("LanguagesController exception: Create method, viewModel object null...");
                return View("CannotCreateLanguage", this.GetBaseViewModel(null));
            }   //if null
            Language language = viewModel.LanguageToOperateWith;
            if (language == null)
            {
                if (viewModel.CurrentLanguage != null)
                {
                    return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("CannotCreateLanguage", this.GetBaseViewModel(null));
            }   //if
            try
            {
                language.language_is_default = false;
                language.language_img = "admin-pl.png";
                language.language_created = DateTime.Now;
                language.language_modified = DateTime.Now;
                if (ModelState.IsValid)
                {
                    Language newLanguage = this._languageDAO.Add(language);
                    if (newLanguage == null)
                    {
                        Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, language failed...");
                        if (viewModel.CurrentLanguage != null)
                        {
                            return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                        }   //if
                        return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                    }
                    Language defaultLanguage = _languageDAO.GetDefaultLanguage();
                    if (newLanguage != null || defaultLanguage != null)
                    {
                        if (!_pageTypeDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, page type failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_templateDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, template failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_pageTypeAcceptableTemplateDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, page type acceptable template failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_variableDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, variable failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_moduleDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, module failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_moduleTemplateDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, module template failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_galleryDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, gallery failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_pageDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, page failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_galleryImageDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, gallery image failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_menuDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, menu failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_menuItemDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, menu item failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_variableValueDAO.InsertForNewLanguage(defaultLanguage.language_id, newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, variable value failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_pageDAO.UpdateLinksForNewLanguage(newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, update links failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                        if (!_menuItemDAO.UpdateLinksForNewLanguage(newLanguage.language_id))
                        {
                            this.DeleteLanguageAndDependencies(newLanguage.language_id);
                            Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, update links failed...");
                            if (viewModel.CurrentLanguage != null)
                            {
                                return View("CannotCreateLanguage", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                            }   //if
                            return View("CannotCreateLanguage", this.GetBaseViewModel(null));
                        }
                    }   //if
                    if (this._languageDAO.SubmitChanges())
                    {
                        if (viewModel.CurrentLanguage != null)
                        {
                            return View("Index", this.GetLanguagesIndexViewModel(viewModel.CurrentLanguage.language_id));
                        }   //
                        Language currentLanguage = this._languageDAO.GetDefaultLanguage();
                        if (currentLanguage != null)
                        {
                            return View("Index", this.GetLanguagesIndexViewModel(currentLanguage.language_id));
                        }   //if
                        return View("Index", this.GetLanguagesIndexViewModel(null));
                    }   //if submit
                }   //if valid
                Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, modelstate not valid...");
                if (viewModel.CurrentLanguage != null)
                {
                    return View("Create", this.GetLanguagesCreateEditDeleteViewModel(viewModel.CurrentLanguage.language_id, language));
                }   //if
                return View("Create", this.GetLanguagesCreateEditDeleteViewModel(null, language));
            }   //try
            catch
            {
                Logger.MyLogger.Log("LanguagesController exception: CreateNewLanguage method, try transaction failed...");
                if (viewModel.CurrentLanguage != null)
                {
                    return View("Create", this.GetLanguagesCreateEditDeleteViewModel(viewModel.CurrentLanguage.language_id, language));
                }   //if
                return View("Create", this.GetLanguagesCreateEditDeleteViewModel(null, language));
            }   //catch
        }



        /// <summary>
        /// Akcja przekierowujaca na ekran potwierdzenia usuniecia wersji jezykowej.
        /// </summary>
        /// <param name="languageId">klucz glowny</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult Delete(int languageId, int? currentLanguageId)
        {
            if (!PermissionHelper.HasUserGlobalPermissionAccess(this.GetAuthenticatedUserId()))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (!currentLanguageId.HasValue)
            {
                Language currentLanguage = this._languageDAO.GetDefaultLanguage(this.GetAuthenticatedUserId());
                if (currentLanguage != null)
                {
                    currentLanguageId = currentLanguage.language_id;
                }   //if
            }   //if
            if (languageId <= 0)
            {
                return View("NoLanguageToDeleteFound", this.GetBaseViewModel(currentLanguageId));
            }   //if
            Language language = this._languageDAO.FindByPK(languageId);
            if (language == null)
            {
                return View("NoLanguageToDeleteFound", this.GetBaseViewModel(currentLanguageId));
            }   //if
            return View(this.GetLanguagesCreateEditDeleteViewModel(currentLanguageId, language));
        }

        /// <summary>
        /// Usuwanie wersji jezykowej.
        /// </summary>
        /// <param name="viewModel">model danych</param>
        /// <returns>odpowiedni widok</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult DeleteConfirmed(LanguagesCreateEditDeleteViewModel viewModel)
        {
            if (!PermissionHelper.HasUserGlobalPermissionAccess(this.GetAuthenticatedUserId()))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (viewModel == null)
            {
                return View("NoLanguageToDeleteFound", this.GetBaseViewModel(null));
            }   //if null
            if (viewModel.LanguageToOperateWith == null)
            {
                return View("NoLanguageToDeleteFound", this.GetBaseViewModel(null));
            }   //if null
            int languageId = viewModel.LanguageToOperateWith.language_id;
            if (languageId <= 0)
            {
                if (viewModel.CurrentLanguage != null)
                {
                    return View("NoLanguageToDeleteFound", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("NoLanguageToDeleteFound", this.GetBaseViewModel(null));
            }   //if
            Language language = this._languageDAO.FindByPK(languageId);
            if (language == null)
            {
                if (viewModel.CurrentLanguage != null)
                {
                    return View("NoLanguageToDeleteFound", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("NoLanguageToDeleteFound", this.GetBaseViewModel(null));
            }   //if
            if (this.DeleteLanguageAndDependencies(languageId))
            {
                if (viewModel.CurrentLanguage != null)
                {
                    return View("Index", this.GetLanguagesIndexViewModel(viewModel.CurrentLanguage.language_id));
                }   //
                Language currentLanguage = this._languageDAO.GetDefaultLanguage(this.GetAuthenticatedUserId());
                if (currentLanguage != null)
                {
                    return View("Index", this.GetLanguagesIndexViewModel(currentLanguage.language_id));
                }   //if
                return View("Index", this.GetLanguagesIndexViewModel(null));
            }   //if
            return View("CannotDeleteLanguage", this.GetBaseViewModel(null));
        }

        /// <summary>
        /// Metoda do usuwania wersji jezykowych.
        /// </summary>
        /// <param name="languageId">id wersji jezykowej</param>
        /// <returns></returns>
        private bool DeleteLanguageAndDependencies(int languageId)
        {
            try
            {
                _userPageTypeDAO.DeleteByLanguageId(languageId);
                _userLanguageDAO.DeleteByLanguageId(languageId);
                _variableValueDAO.DeleteByLanguage(languageId);
                _menuItemDAO.DeleteByLanguage(languageId);
                _menuDAO.DeleteByLanguage(languageId);
                _galleryImageDAO.DeleteByLanguage(languageId);
                _pageDAO.DeleteByLanguage(languageId);
                _galleryDAO.DeleteByLanguage(languageId);
                _moduleTemplateDAO.DeleteByLanguage(languageId);
                _moduleDAO.DeleteByLanguage(languageId);
                _variableDAO.DeleteByLanguage(languageId);
                _pageTypeAcceptableTemplateDAO.DeleteByLanguage(languageId);
                _templateDAO.DeleteByLanguage(languageId);
                _pageTypeDAO.DeleteByLanguage(languageId);
                if (this._languageDAO.Delete(languageId))
                {
                    return (this._languageDAO.SubmitChanges());
                }   //if
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>LanguagesIndexViewModel</code>.
        /// </summary>
        /// <param name="page">biezaca strona</param>
        /// <returns>odpowiedni model danych</returns>
        private LanguagesIndexViewModel GetLanguagesIndexViewModel(int? page, int? currentLanguageId)
        {
            if (page == null)
            {
                page = 1;
            }   //if
            Language currentLanguage = null;
            if (!currentLanguageId.HasValue)
            {
                currentLanguage = this._languageDAO.GetDefaultLanguage(this.GetAuthenticatedUserId());
                if (currentLanguage != null)
                {
                    currentLanguageId = currentLanguage.language_id;
                }   //if
            }
            else
            {   //if lang id null
                currentLanguage = this._languageDAO.FindByPK(currentLanguageId);
            }   //else lang id null
            return new LanguagesIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                    this.GetPageTypesInMenu(currentLanguageId),
                    this.GetMenus(currentLanguageId),
                    Pagination.ToPagedList<Language>(this._languageDAO.FindAll(), page ?? 1), 
                    this.GetCurrentLanguage(currentLanguageId));
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>LanguagesIndexViewModel</code>.
        /// </summary>
        /// <param name="page">biezaca strona</param>
        /// <returns>odpowiedni model danych</returns>
        private LanguagesIndexViewModel GetLanguagesIndexViewModel(int? currentLanguageId)
        {
            int? page = 1;
            Language currentLanguage = null;
            if (!currentLanguageId.HasValue)
            {
                currentLanguage = this._languageDAO.GetDefaultLanguage(this.GetAuthenticatedUserId());
                if (currentLanguage != null)
                {
                    currentLanguageId = currentLanguage.language_id;
                }   //if
            }
            else
            {   //if lang id null
                currentLanguage = this._languageDAO.FindByPK(currentLanguageId);
            }   //else lang id null
            return new LanguagesIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                    this.GetPageTypesInMenu(currentLanguageId),
                    this.GetMenus(currentLanguageId),
                    Pagination.ToPagedList<Language>(this._languageDAO.FindAll(), page ?? 1),
                    this.GetCurrentLanguage(currentLanguageId));
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>LanguagesCreateEditDeleteViewModel</code>.
        /// </summary>
        /// <returns>odpowiedni model danych</returns>
        private LanguagesCreateEditDeleteViewModel GetLanguagesCreateEditDeleteViewModel(int? currentLanguageId, Language language)
        {
            Language currentLanguage = null;
            if (!currentLanguageId.HasValue)
            {
                currentLanguage = this._languageDAO.GetDefaultLanguage(this.GetAuthenticatedUserId());
                if (currentLanguage != null)
                {
                    currentLanguageId = currentLanguage.language_id;
                }   //if
            }   //if
            return new LanguagesCreateEditDeleteViewModel(this.GetLanguagesToDropDown(currentLanguageId), 
                    this.GetPageTypesInMenu(currentLanguageId),
                    this.GetMenus(currentLanguageId),
                    language, this.GetCurrentLanguage(currentLanguageId));
        }

        /// <summary>
        /// Metoda odpowiadajaca za operacje zmiany wersji jezykowej w drop downie.
        /// </summary>
        /// <param name="viewModel">model danych</param>
        /// <returns>odpowiedni widok</returns>
        [Authorize]
        public ActionResult ChangeLanguage(LanguagesIndexViewModel viewModel)
        {
            if (viewModel == null)
            {
                Logger.MyLogger.Log("LanguagesController exception: ChangeLanguage method, viewModel object null...");
                return View("NoLanguageFound", this.GetBaseViewModel(null));
            }   //if null
            return View("Index", this.GetLanguagesIndexViewModel(viewModel.DropDownLanguageId));
        }

    }
}
